package com.mvvmhabit.org.utils

import android.app.Application
import android.content.ComponentCallbacks
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatActivity

/**
 * Android 适配方案
 */
class DisplayUtil {
    companion object {

        /**
         * 适配：修改设备密度
         */
        private var sNoncompatDensity = 0f
        private var sNoncompatScaledDensity = 0f

        fun setCustomDensity(activity: AppCompatActivity, application: Application) {
            val appDisplayMetrics = application.resources.displayMetrics
            if (sNoncompatDensity == 0f) {
                sNoncompatDensity = appDisplayMetrics.density
                sNoncompatScaledDensity = appDisplayMetrics.scaledDensity
                // 防止系统切换后不起作用
                application.registerComponentCallbacks(object : ComponentCallbacks {

                    override fun onConfigurationChanged(newConfig: Configuration) {
                        if (newConfig != null && newConfig.fontScale > 0) {
                            sNoncompatScaledDensity =
                                application.resources.displayMetrics.scaledDensity
                        }
                    }

                    override fun onLowMemory() {}
                })
            }
            val targetDensity = (appDisplayMetrics.widthPixels / 360).toFloat()
            // 防止字体变小
            val targetScaleDensity = targetDensity * (sNoncompatScaledDensity / sNoncompatDensity)
            val targetDensityDpi = (160 * targetDensity).toInt()
            appDisplayMetrics.density = targetDensity
            appDisplayMetrics.scaledDensity = targetScaleDensity
            appDisplayMetrics.densityDpi = targetDensityDpi
            val activityDisplayMetrics = activity.resources.displayMetrics
            activityDisplayMetrics.density = targetDensity
            activityDisplayMetrics.scaledDensity = targetScaleDensity
            activityDisplayMetrics.densityDpi = targetDensityDpi
        }
    }
}